#include "DecompressDirector.h"
#include <iostream>
using namespace std;

DecompressDirector::DecompressDirector(string filename)
{
	_codedFile = filename;
}

DecompressDirector::~DecompressDirector(void)
{
}

bool DecompressDirector::Decompress(FileReader* r, Decoder* d, FileWriter* w)
{
	cout << "> Will decode " << _codedFile << endl;
	if(r->OpenFile(_codedFile) == false)
		return false;
	_outFile = r->ReadString();
	cout << "> The original file is " << _outFile << ", will make it." << endl;
	if(w->OpenFile(_outFile) == false)
		return false;
	cout << "> Read dictionary.." << endl;
	if(d->ReadDictionary(r) == false)
		return false;
	cout << "> Decoding.." << endl;
	d->SetCodeCount(r->ReadInt());
	BYTE codes[256];
	while(!r->Eof())
	{
		int count = 0;
		while(count < 256 && !r->Eof())
		{
			BYTE b = r->ReadByte();
			if(r->Good())
				codes[count++] = b;
		}
		d->SetCodeBlock(codes, count);
		DictData dd;
		while(d->GetNextData(&dd))
			w->WriteByte(dd._data, dd._dataSize);
	}

	r->CloseFile();
	w->CloseFile();
	return true;
}